package com.jcl.recruit.elasticsearch.resume.service;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.elasticsearch.core.entity.vo.SearchResumeRecord;
import com.jcl.recruit.elasticsearch.resume.logic.RecruitElasticLogic;
import com.jcl.recruit.elasticsearch.resume.logic.ResumeCommonLogic;
import com.jcl.recruit.elasticsearch.resume.logic.SearchResumeBasicInfoLogic;
import com.jcl.recruit.elasticsearch.resume.model.SearchResumeBasicInfo;
import com.jcl.recruit.elasticsearch.resume.po.ResumeResultEntity;
import com.jcl.recruit.elasticsearch.resume.po.ResumeSimpleResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

/**
 * @ClassName ResumeBaseService

 * @Author yyliu
 * @Date 2018/11/13 10:20
 * @Version 1.0
 **/
@Service
public class ResumeService {

    @Autowired
    private ResumeCommonLogic resumeCommonLogic;

    @Autowired
    private RecruitElasticLogic recruitElasticLogic;

    @Autowired
    private SearchResumeBasicInfoLogic searchResumeBasicInfoLogic;

    public PageResult pageResult(List<SearchResumeRecord> recordList,
                                 String channel,
                                 String corpCode,
                                 Integer curPage,
                                 Integer resumeUpdateDays,
                                 Integer comingDays) {
        PageResult pageResult = recruitElasticLogic.searchResumePage(recordList, channel,corpCode,
                curPage, resumeUpdateDays, comingDays, null);

        List<Map<String, Object>> resumes = pageResult.getData();
        if (CollectionUtils.isNotEmpty(resumes)) {
            List<Integer> resumeIds = new ArrayList<>();
            for (Map<String, Object> map : resumes) {
                String _id = map.get("id").toString();
                if(_id.contains(".")){
                    _id = _id.substring(0, _id.indexOf("."));
                }
                resumeIds.add(Integer.valueOf(_id));
            }
            List<ResumeSimpleResultEntity> entityList = resumeCommonLogic.findSimpleList(resumeIds);

            pageResult.setData(entityList);

        } else {
            pageResult = new PageResult(curPage, DEFAULT_PAGE_SIZE, 0, resumes);
        }

        return pageResult;
    }

    public List<Map<String, String>> getCountBySources(Boolean today){

        return searchResumeBasicInfoLogic.selectCountBySources(today);
    }


    /**
     * @MethodName findResumeDetail
     * //TODO
     * @Param [resumeId]
     * @Return ResumeResultEntity
     * @Author 63404
     * @Date 2018/11/16
     **/
    public ResumeResultEntity findResumeDetail(int resumeId) {

        return resumeCommonLogic.findDetailEntity(resumeId);
    }

    /**
     * @MethodName findResumeDetail
     * //TODO
     * @Param [resumeId]
     * @Return ResumeResultEntity
     * @Author 63404
     * @Date 2018/11/16
     **/
    public ResumeSimpleResultEntity findResumeSimpleDetail(int resumeId) {

        return resumeCommonLogic.findSimpleEntity(resumeId);
    }

    /**
     * @MethodName findResumeDetail
     * //TODO
     * @Param [resumeId]
     * @Return ResumeResultEntity
     * @Author 63404
     * @Date 2018/11/16
     **/
    public SearchResumeBasicInfo getBasicInfo(int resumeId) {

        SearchResumeBasicInfo basicInfo = searchResumeBasicInfoLogic.findOne(resumeId);
        return basicInfo;
    }


}
